<template>
  <div class="upload">
    <el-upload
      class="upload-demo"
      drag
      action="#"
      :multiple="false"
      accept=".docx,.xlsx,.pdf"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    </el-upload>
    <hr />
    <el-form :model="form">
      <el-row :gutter="20">
        <el-col :span="20">
          <el-form-item>
            <el-input
              v-model="form.url"
              clearable
              placeholder="在线文件预览...http"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" @click="viewFileClick">预览</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "upload",
  data() {
    return {
      form: {
        url: "",
      },
    };
  },
  methods: {
    beforeUpload(file) {
      this.$emit("beforeUpload", file, file.name.split(".").pop());
    },
    viewFileClick() {
      const parts = this.form.url.split("/"); // 分割链接
      const lastPart = parts[parts.length - 1]; // 获取最后一部分
      const splitLastPart = lastPart.split("."); // 分割最后一部分
      const fileExtension = splitLastPart[splitLastPart.length - 1];
      this.$emit("viewFile", this.form.url, fileExtension);
    },
  },
};
</script>
<style scoped lang="scss"></style>